package com.yangtao.springbootfreemarker.web.controller.sys;

import com.yangtao.core.exception.ServiceException;
import com.yangtao.core.jpa.dao.SpecificationBuilder;
import com.yangtao.core.jpa.dao.SpecificationOperator;
import com.yangtao.core.utils.ParameterUtil;
import com.yangtao.core.utils.StringUtil;
import com.yangtao.core.utils.bean.BeanReadUtil;
import com.yangtao.core.web.ResultMsg;
import com.yangtao.springbootfreemarker.domain.sys.entity.SysUser;
import com.yangtao.springbootfreemarker.domain.sys.service.SysUserService;
import com.yangtao.springbootfreemarker.web.controller.common.ControllerBase;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/sysUser")
public class SysUserController extends ControllerBase {

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 获取用户列表
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions(value = { "springboot:sys:user:list"}, logical = Logical.OR)
	@ResponseBody
	public ResultMsg list() {

		SpecificationBuilder<SysUser> sb = new SpecificationBuilder<>();
		//项目名称
		String name = ParameterUtil.getParamStr(request, "name");
		if (StringUtil.isNotEmpty(name)) {
			sb.andWhere("name", "%" + name + "%", SpecificationOperator.Like);
		}
		Page<SysUser> page = sysUserService.findPage(buildPage(), sb.getSpecifications());
		return ResultMsg.buildSuccessMsg(page);
	}

	/**
	 * 获取用户信息
	 * @return
	 */
	@RequestMapping("/info")
	@RequiresPermissions(value = { "springboot:sys:user:info"}, logical = Logical.OR)
	@ResponseBody
	public ResultMsg info() {
		String id = ParameterUtil.getParamStr(request, "id");
		if(StringUtil.isEmpty(id)) {
			throw new ServiceException("编号为空");
		}
		SysUser sysUser = sysUserService.findOne(id);
		if(sysUser == null) {
			throw new ServiceException("数据不存在");
		}
		sysUser.setSalt(null);
		sysUser.setPassword(SysUser.SHOW_PASSWORD);
		return ResultMsg.buildSuccessMsg(sysUser);
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/create")
	@RequiresPermissions(value = { "springboot:sys:user:create"}, logical = Logical.OR)
	@ResponseBody
	public ResultMsg create() {
		SysUser data = new SysUser();
		data = BeanReadUtil.readFromRequest(request, data);
		sysUserService.insert(data);
		return ResultMsg.buildSuccessMsg(data);
	}

	/**
	 * 修改
	 * @return
	 */
	@RequestMapping("/update")
	@RequiresPermissions(value = { "springboot:sys:user:update"}, logical = Logical.OR)
	@ResponseBody
	public ResultMsg update() {
		String id = ParameterUtil.getParamStr(request, "id");
		if(StringUtil.isBlank(id)) {
			throw new ServiceException("编号为空");
		}
		SysUser data = sysUserService.findOne(id);
		data = BeanReadUtil.readFromRequest(request, data);
		sysUserService.update(data);
		return ResultMsg.buildSuccessMsg(data);
	}

	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions(value = { "springboot:sys:user:delete"}, logical = Logical.OR)
	@ResponseBody
	public ResultMsg delete() {
		String userId = ParameterUtil.getParamStr(request, "userId");
		sysUserService.deleteById(userId);
		return ResultMsg.buildSuccessMsg();
	}
}
